package org.genntii.warehousesystem.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.genntii.warehousesystem.common.Result;
import org.genntii.warehousesystem.domain.dto.*;
import org.genntii.warehousesystem.service.UserService;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

@Tag(name = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public Result<Map<Object,Object>> register(@RequestBody UserRegisterDTO user){
        return Result.success(userService.register(user.getUsername(),user.getPassword()));
    }

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<Map<Object,Object>> login(@RequestBody LoginDTO user){
        return Result.success(userService.login(user.getUsername(),user.getPassword()));
    }

    @Operation(summary = "权限测试接口")
    @GetMapping("/test")
    public String test(HttpServletRequest request){
        int id = (int) request.getAttribute("id");
        System.out.println(id);
        return "你好"+id;
    }

    @Operation(summary = "检查是否实名")
    @GetMapping("/isRealName")
    public Result isRealName(HttpServletRequest request){
        int id = (int) request.getAttribute("id");
        if (userService.userIsRealName(id)){
            return Result.success();
        }else {
            return Result.error("需要实名");
        }
    }

    @Operation(summary = "进行实名制")
    @PostMapping("/realName")
    public Result realName(HttpServletRequest request, @RequestBody RealNameDTO realNameDTO){
        int id = (int) request.getAttribute("id");
        userService.realName(realNameDTO.getName(),realNameDTO.getIdentity(),id);
        return Result.success();
    }

    @Operation(summary = "检查是否绑定银行账号")
    @GetMapping("/isBank")
    public Result isBank(HttpServletRequest request){
        int id = (int) request.getAttribute("id");
        if (userService.userIsBank(id)){
            return Result.success();
        }else {
            return Result.error("需要绑定银行卡");
        }
    }

    @Operation(summary = "进行绑定银行")
    @PostMapping("/bank")
    public Result bank(HttpServletRequest request, @RequestBody BankDTO bankDTO){
        int id = (int) request.getAttribute("id");
        userService.bank(bankDTO.getAccount(),bankDTO.getName(),id, LocalDateTime.now());
        return Result.success();
    }

    @Operation(summary = "检查是否绑定手机号")
    @GetMapping("/isTel")
    public Result isTel(HttpServletRequest request){
        int id = (int) request.getAttribute("id");
        if (userService.userIsTel(id)){
            return Result.success();
        }else {
            return Result.error("需要绑定手机号");
        }
    }

    @Operation(summary = "进行手机号绑定")
    @PostMapping("/tel")
    public Result tel(HttpServletRequest request, @RequestBody TelDTO telDTO){
        int id = (int) request.getAttribute("id");
        userService.tel(telDTO.getTel(),LocalDateTime.now(),id);
        return Result.success();
    }

    @Operation(summary = "常用收货地址更新")
    @Parameter(name = "addressId",description = "收货地址id",required = true,in = ParameterIn.QUERY)
    @PostMapping("addressUpdate")
    public Result addressUpdate(HttpServletRequest request, @RequestParam("addressId") int addressId){
        int id = (int) request.getAttribute("id");
        userService.setCommonlyAddress(id,addressId);
        return Result.success();
    }

}
